{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 27 - The data science process revisited\n", "\n", "Today we will look at a dataset about health insurance policies.\n", "\n", "First download the dataset from GitHub: [https://github.com/stedy/Machine-Learning-with-R-datasets/blob/master/insurance.csv](https://github.com/stedy/Machine-Learning-with-R-datasets/blob/master/insurance.csv)\n", "\n", "In this data, each row represents an insurance policy and the 7 columns contain the following information about the individual:\n", "\n", "age: age of policy holder\n", "\n", "sex: sex of policy holder\n", "\n", "bmi: boday mass index (bmi) of policy holder. bmi is a (sometimes unreliable) measurement of body fat in adults\n", "\n", "children: number of children (dependents) on the policy\n", "\n", "smoker: whether the policy holder is a smoker\n", "\n", "region: region of the country the policy holder lives in\n", "\n", "charges: price for insurance policy\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import statsmodels.formula.api as smf\n", "import seaborn as sns\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1338, 7)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "insurance = pd.read_csv(\"../../data/insurance.csv\")\n", "insurance.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exploratory data analysis\n", "\n", "Plot a bar chart of the region column." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot a histogram of the ages." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the median number of children?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Are there more smokers or non-smokers?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Probabilities\n", "\n", "What's the probability that a woman has 3 or more children?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's the probability that a person paying more than $7000 for the policy is from the northeast>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparing distributions\n", "\n", "Compare the distribution of bmi for smokers and non-smoker using overlapping histograms. Do you notice any differences?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare the distribution of ages for policy holders from the northeast and southeast. Do you notice any differences?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hypothesis testing\n", "\n", "Does the proportion of males (or females) differ from 0.5 in this data? Use a hypothesis test to find out." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bootstrap \n", "\n", "What's the mean age in this sample, and what is the confidence interval for this mean?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Correlation\n", "\n", "Compute the correlation between age, bmi, number of children, and policy cost." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Seaborn library to plot the scatter plots for each pair of numerical columns. What do you notice?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear regression\n", "\n", "Use linear regression to make a model for predicting the policy costs, using the quantitative data. How good is this model?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use Seaborn to plot the regression line using just age to predict the policy cost. What do you notice?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Maybe it would be more useful to predict two different regression lines, one for smokers and one for non-smokers. Try this." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### k-nearest neighbors\n", "\n", "Predict whether someone is a smoker based on the quantitative data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }